<script setup>
import {ref} from "vue";
import qs from 'qs';
import axios from "axios";
import {ElMessage} from "element-plus";
import router from "@/router";
const user =ref({
  username:"",
  password:"",
})
const login =()=>{
  let data = qs.stringify(user.value);
  axios.post('http://localhost:8080/v1/users/login',data)
      .then((response)=>{
        if (response.data.code==2001) {
          ElMessage.success(user.value.username+',您已成功登录');
          user.value = response.data.data;
          //将数据保存到前端
          localStorage.setItem("user",JSON.stringify(user.value))
          router.push("/");
        }else {
          ElMessage.error(response.data.msg);
        }
      })
}
</script>

<template>
<!--登录页-->
  <div id ="main-div" >
  <el-card style="width: 500px ;height: 250px; margin: 0 auto;position: relative ;top: 100px" >
    <el-form label-width="60px" style="margin-top: 50px;">
      <el-form-item label="用户名"><el-input v-model="user.username" placeholder="请输入用户名"></el-input></el-form-item>
      <el-form-item label="密码"><el-input  v-model="user.password" placeholder="请输入密码" type="password"></el-input></el-form-item>
      <el-form-item ><el-button type="primary" style="margin-left: 150px" @click="login">登录</el-button></el-form-item>
    </el-form>
  </el-card>
  </div>
</template>

<style scoped>
#main-div {
  height: 500px;
  background-image: url('/public/imgs/loginbg.gif');
  /*常用于设置封面*/
  background-size: cover;
  background-position: center;
}
</style>